package com.dzt.androidframework.photo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.photo.adapter.GridViewAdapter;
import com.dzt.androidframework.utils.JToastKit;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class SelectMoreActivity extends ActivityTitle implements AdapterView.OnItemClickListener {
	/**
	 * 最大上传图片数量
	 */
	public static final int MAX_IMAGECOUNT = 9;
	@ViewInject(R.id.gv_select_more)
	private GridView imageGridView;

	private int imageCount = 0; // 已在上传列表中的图片数量

	private List<ImageModel> imageModelList; // 当前页显示的List
	private GridViewAdapter adapter;
	private Intent intent;
	private ArrayList<String> returnPathList; // 返回前一页时传送的list

	@Override
	protected int getLayoutId() {
		return R.layout.activity_select_more;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.text_camera_more);

		imageGridView.setOnItemClickListener(this);

		intent = getIntent();
		imageCount = intent.getExtras().getInt("imageCount"); // 获取前页待上传数量
		returnPathList = intent.getExtras().getStringArrayList("photoArrayList"); // 获取前页待上传路径集合,存入待返回的集合

		// TODO 存在问题:没有兼容所有手机的相册存储路径，测试在畅享5手机上该界面为空白，红米2A则可以显示
		//             此问题我暂时还没细查，可以让用户单选或者拍照。
		// 获取默认相机的存储路径
		String DCIMPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString();
		String cameraPath = DCIMPath + File.separator + "Camera";

		// 获取默认相机路径下所有图片文件名
		List<String> allImageFormCamera = getImageFileName(cameraPath);
		imageModelList = new ArrayList<>();
		ImageModel imageModel;
		for (String string : allImageFormCamera) {
			imageModel = new ImageModel();
			imageModel.setImagePath(string);
			imageModel.setImageState(false);
			imageModelList.add(imageModel);
		}

		List<String> equalsList = new ArrayList<>(); // 相同数据（之前选择过的数据）
		// 遍历传入的路径，若路径完全相同，则为之前选择过的，将对象的选中状态设为true
		for (String intentStr : returnPathList) {
			for (int i = 0; i < imageModelList.size(); i++) {
				ImageModel imageModel2 = imageModelList.get(i);
				if (imageModel2.getImagePath().equals(intentStr)) {
					imageModel2.setImageState(true);
					imageModelList.set(i, imageModel2);
					equalsList.add(intentStr);
				}
			}
		}
		returnPathList.removeAll(equalsList); // 移除返回List中处理过的相同值

		adapter = new GridViewAdapter(this, imageModelList, View.VISIBLE);
		imageGridView.setAdapter(adapter);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	/**
	 * 获取指定目录下所有指定类型的文件
	 *
	 * @param filePath 指定路径
	 */
	private List<String> getImageFileName(String filePath) {
		List<String> fileList = new ArrayList<>();
		File file = new File(filePath);
		File[] subFile = file.listFiles();

		for (int i = 0; i < subFile.length; i++) {
			// 判断是否为文件夹
			if (!subFile[i].isDirectory()) {
				String fileName = subFile[i].getName();
				String LowerFileName = fileName.trim().toLowerCase(Locale.ENGLISH);
				if (LowerFileName.endsWith(".jpg") || LowerFileName.endsWith(".jpeg") || LowerFileName.endsWith(".png")) {
					fileList.add(filePath + File.separator + fileName);
				}
			}
		}
		return fileList;
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Event(value = {R.id.btn_ok, R.id.btn_cancel})
	private void onViewClick(View v) {
		switch (v.getId()) {
			case R.id.btn_ok:
				for (ImageModel imageModel : imageModelList) { // 遍历当前界面imageModelList
					if (imageModel.isImageState()) { // 如果选择状态是true
						returnPathList.add(imageModel.getImagePath()); // 则将路径添加至返回时传送的list
					}
				}
				// 设置intent传送参数，返回前一页
				intent = new Intent();
				intent.putStringArrayListExtra("returnPathList", returnPathList);
				setResult(Activity.RESULT_OK, intent);
				finish();
				break;
			case R.id.btn_cancel:
				break;
			default:
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
		ImageModel imageModel = imageModelList.get(i);
		if (imageModel.isImageState()) {
			imageModel.setImageState(false);
			imageCount -= 1;
		} else if (imageCount < MAX_IMAGECOUNT) {
			imageModel.setImageState(true);
			imageCount += 1;
		} else {
			JToastKit.getInstance().showMessage("单次最多上传" + MAX_IMAGECOUNT + "张图片");
		}
		imageModelList.set(i, imageModel);
		adapter.notifyDataSetChanged();
	}
}
